<template>
	<view>
		<swiper class="link-breakthrough" :indicator-dots="true" indicator-active-color="#E69038" indicator-color='#fff' :autoplay="true"
			:interval="3000" :duration="1000" circular="true">
			<swiper-item v-for="(item,index) in swiperList" :key="index" @click="toNavPage(item.page)">
				<view class="swiper-item">
					<image :src="item.imgUrl" mode="aspectFill"></image>
				</view>
			</swiper-item>
		</swiper>

		<!-- 头部 -->
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item"
				:class="{ current: tabCurrentIndex === index }" @click="tabClick(item._id,index)">
				{{ item.title }}
			</view>
		</view>
		<!-- 显示区域 -->
		<view class="list" v-for="(item, index) in navList" :key="index" v-if="tabCurrentIndex === index">
			<view class="article-item" v-for="(item,index) in artList" :key="index" @click="toArtDetail(item._id)">
				<view class="art-img">
					<image :src="item.imgUrl" class="art-pic"></image>
				</view>
				<view class="desc">
					<view class="title">
						{{item.title}}
					</view>
					<view class="info">
						{{item.createDate}} &nbsp; · &nbsp; {{item.memo}}
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				tabCurrentIndex: 0,
				firstNavId: '', // 第一个分类id
				artList: [], // 文章列表
				navList: [],
				swiperList: [
					{
						imgUrl:'',
						page:''
					},
					{
						imgUrl:'',
						page:''
					}
				]
			};
		},
		onLoad(options) {
			
			
		},
		onShow() {
			// 页面显示是默认选中第一个
			this.tabCurrentIndex = 0;
			
			this.getCultureCate()
			this.getPicTop()
			this.getPicDTHB()
		},
		methods: {
			async getPicTop() {
				const {
					data: res
				} = await uni.$http.post('/dictionary/getConfig', {
					code: 'WX_IMAGE'
				})
				this.swiperList[0].imgUrl=res.data
				this.swiperList[0].page='/pages/guide/guide'
			},
			async getPicDTHB() {
				const {
					data: res
				} = await uni.$http.post('/dictionary/getConfig', {
					code: 'DTHB_IMG'
				})
				console.log(res,'DTHB')
				this.swiperList[1].imgUrl=res.data
				this.swiperList[1].page='/subpkg/Qa/Qa'
				
			},
			
			toNavPage(page){
				if(page=='/subpkg/Qa/Qa'&&JSON.stringify(this.userInfo) == '{}'){
					uni.navigateTo({
						url:'/subpkg/login/login'
					})
					uni.$showMsg('请先登录！')
				}else{
					// uni.reLaunch({
					// 	url:page
					// })
					// uni.redirectTo({
					// 	url:page
					// })
					uni.navigateTo({
						url:page
					})
					uni.setStorageSync('fromPage','culture')
				}
			},
			// 获取资讯分类
			async getCultureCate() {
				const {
					data: res
				} = await uni.$http.post('/informationType/list')
				console.log(res.data)
				this.navList = res.data
				this.firstNavId = res.data[0]._id
				this.getArtList(this.firstNavId)
			},
			// 根据分类获取文章
			async getArtList(id) {
				const {
					data: res
				} = await uni.$http.post('/information/list', {
					"category": id,
					"offset": 1,
					"limit": 10
				})
				this.artList = res.data.list
				this.artList.forEach(item => {
					item.createDate = item.createDate.slice(0, 10)
				})
			},
			changeTab(e) {
				this.tabCurrentIndex = e.target.current;
			},
			//顶部tab点击
			tabClick(id, index) {
				this.tabCurrentIndex = index;
				this.getArtList(id)
			},
			// 去文章详情
			toArtDetail(id) {
				uni.navigateTo({
					url: "/subpkg/art-detail/art-detail?id=" + id
				})
			},
			// 答题闯关
			toBreakThrough() {
				if (JSON.stringify(this.userInfo) == '{}') {
					uni.navigateTo({
						url:'/subpkg/login/login'
					})
					uni.$showMsg('请先登录！')
					return
				}
				uni.navigateTo({
					url: '/subpkg/Qa/Qa'
				})
			}
		},
		computed: {
			...mapState('m_user', ['userInfo'])
		}
	}
</script>

<style lang="scss">
	.link-breakthrough {
		width: 100%;
		height: 341rpx;

		image {
			width: 100%;
			height: 341rpx
		}
	}

	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		// box-shadow: 0 1px 5px rgba(0, 0, 0, 0.06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: #999;
			position: relative;

			&.current {
				color: #333;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 5rpx solid #D80E0E;
				}
			}
		}
	}

	.list {
		padding: 20rpx 24rpx;
		box-sizing: border-box;
	}


	.article-item {
		height: 166rpx;
		border-bottom: 1rpx solid #ECECEC;
		padding: 20rpx 0;
		box-sizing: border-box;
		display: flex;

		.art-img {
			width: 190rpx;
			height: 126rpx;
			border-radius: 6rpx;
			margin-right: 16rpx;

			.art-pic {
				width: 190rpx;
				height: 126rpx;
				border-radius: 6rpx;
			}
		}

		.desc {
			flex: 1;
		}

		.info {
			display: flex;
			font-size: 24rpx;
			color: #777777;
			margin-top: 15rpx;
		}

		.title {
			font-size: 28rpx;
			font-weight: 500;
			color: #333333;
			height: 60rpx;
			overflow: hidden;
			margin-bottom: 10rpx;
		}
	}
</style>
